package com.yifushidai.api;

import com.yifushidai.annotation.IgnoreAuth;
import com.yifushidai.annotation.UserHolder;
import com.yifushidai.service.UserService;
import com.yifushidai.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "用户")
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @IgnoreAuth
    @PostMapping("/login")
    @ApiOperation(value = "短信登录(忽略token)", produces = MediaType.APPLICATION_JSON_VALUE)
    public R shisuLogin(@ApiParam(value = "手机号", required = true) @RequestParam String mobile,
                        @ApiParam(value = "短信验证码", required = true) @RequestParam String checkCode,
                        @ApiParam(value = "app类型(ios,android)", required = true) @RequestParam String appType,
                        HttpServletRequest request) throws Exception {
        return userService.shisuLogin(mobile, checkCode, appType, request);
    }

//    @IgnoreAuth
//    @PostMapping("/apartmentLogin")
//    @ApiOperation(value = "公寓版登录接口", notes = "登录接口(忽略token)", produces = MediaType.APPLICATION_JSON_VALUE)
//    public R apartmentLogin(@ApiParam(value = "电话号", required = true) @RequestParam String mobile,
//                            @ApiParam(value = "短信验证码", required = true) @RequestParam String checkCode,
//                            @ApiParam(value = "app类型(ios,android)", required = true) @RequestParam String appType) throws Exception {
//        return userService.apartmentLogin(mobile, checkCode, appType);
//    }

    @IgnoreAuth
    @PostMapping("/pwdLogin")
    @ApiOperation(value = "密码登录(忽略token)", produces = MediaType.APPLICATION_JSON_VALUE)
    public R pwdLogin(@ApiParam(value = "手机号", required = true) @RequestParam String mobile,
                      @ApiParam(value = "密码", required = true) @RequestParam String password,
                      HttpServletRequest request) throws Exception {
        return userService.pwdLogin(mobile, password, request);
    }

    @IgnoreAuth
    @PostMapping("/resetPwd")
    @ApiOperation(value = "短信重置密码(忽略token)", produces = MediaType.APPLICATION_JSON_VALUE)
    public R resetPwd(@ApiParam(value = "手机号", required = true) @RequestParam String mobile,
                      @ApiParam(value = "短信验证码", required = true) @RequestParam String checkCode) throws Exception {
        return userService.resetPwd(mobile, checkCode);
    }

    @PostMapping("/modPwd")
    @ApiOperation(value = "修改密码接口", produces = MediaType.APPLICATION_JSON_VALUE)
    public R modPwd(@ApiParam(value = "手机号", required = true) @RequestParam String mobile,
                    @ApiParam(value = "旧密码", required = true) @RequestParam String oldPwd,
                    @ApiParam(value = "新密码", required = true) @RequestParam String newPwd,
                    @ApiParam(value = "短信验证码", required = true) @RequestParam String checkCode) throws Exception {
        return userService.modPwd(mobile, oldPwd, newPwd);
    }

    @PostMapping("/modMobile")
    @ApiOperation(value = "短信修改用户手机号（账号转让）", produces = MediaType.APPLICATION_JSON_VALUE)
    public R modMobile(@ApiParam(value = "旧手机号", required = true) @RequestParam String oldMobile,
                       @ApiParam(value = "新手机号", required = true) @RequestParam String newMobile,
                       @ApiParam(value = "短信验证码", required = true) @RequestParam String checkCode) throws Exception {
        return userService.modMobile(oldMobile, newMobile, checkCode);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "退出接口", notes = "退出", produces = MediaType.APPLICATION_JSON_VALUE)
    public R logout() throws Exception {
        UserHolder.remove();
        return R.ok();
    }
}
